//
//  TTHomeController.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/19.
//  Copyright © 2016年 陶路路. All rights reserved.
//

import UIKit

class TTHomeController: TTBaseViewController{
    
    lazy var ttHomeModel:TTHomeVcModel = TTHomeVcModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin{
            
            visitorView.setImageAndTitle(nil,title: nil)
            return
        }
        
        getData()
        setUpTable()
    }
    
    func setUpTable(){
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.registerClass(TTHomeViewCell.self, forCellReuseIdentifier: "cell")
        tableView.separatorStyle = .None
        self.tableView.estimatedRowHeight = 200
        tableView.tableFooterView = indicatorView
        tableView.addSubview(refresh)
        refresh.addTarget(self, action: "didRefresh", forControlEvents: UIControlEvents.ValueChanged)
        navigationController!.view.insertSubview(refreshText, belowSubview: (navigationController?.navigationBar)!)
    }
//   请求数据
    
    func didRefresh(){
        
        getData()
    }
        
    func getData(){
        
        ttHomeModel.loadHomeData(indicatorView.isAnimating()) { (isSuccess, count) -> () in
            
            self.indicatorView.stopAnimating()
            self.refresh.endRefreshing()
            if !isSuccess{
                
                return
            }
            self.tableView.reloadData()
            
            if count >= 0{
                
                self.getAnimationWith(count)
            }
        }
        
    }
    

    func getAnimationWith(count:Int){
        
        if self.refreshText.hidden == false{
            
            return
        }
        
        var name = ""
        
        if count == 0{
            
            name = "已经是最新数据"
            
        }else{
            
            name = "已为您刷新\(count)条数据"
        }
        self.refreshText.hidden = false
        
        self.refreshText.text = name
        
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            
            self.refreshText.transform = CGAffineTransformMakeTranslation(0, 64)
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.5, delay: 0.5, options: [], animations: { () -> Void in
                    
                    self.refreshText.transform = CGAffineTransformIdentity
                    
                    }, completion: { (_) -> Void in
                  
                        self.refreshText.hidden = true
                })
        }
    }


//   下拉刷新
    private lazy var indicatorView :UIActivityIndicatorView = {
       let ind = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        ind.color = UIColor.orangeColor()
        return ind
    }()
    
    private lazy var refresh:SHRefreshControl = SHRefreshControl()
    
    private lazy var refreshText:UILabel = {
            let text = UILabel()
        text.frame = CGRectMake(0, 0, SCREENW, 40)
        text.font = UIFont.systemFontOfSize(15)
        text.backgroundColor = UIColor.orangeColor()
        text.textAlignment = .Center
        text.textColor = UIColor.whiteColor()
        text.text = "刷新啦啊啦啦啦"
        text.hidden = true
        return text
    }()
//    private lazy var refresh:TTRefreshController = TTRefreshController()
    
}

// tableView 代理方法

extension TTHomeController:UITableViewDataSource,UITableViewDelegate{
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return (self.ttHomeModel.dataArray.count) ?? 0
    }
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? TTHomeViewCell
        
        cell!.backgroundColor = UIColor.whiteColor()
        cell?.ttHomeModel = self.ttHomeModel.dataArray[indexPath.row]
        return cell!
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.row == (self.ttHomeModel.dataArray.count) - 1 && !indicatorView.isAnimating(){
            
            indicatorView.startAnimating()
            
            getData()
        }
    }
    
}